<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>OAuth2 授权引导</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            display: flex;
            justify-content: center;
            align-items: center;
            padding: 20px;
        }
        .container {
            background: white;
            border-radius: 20px;
            box-shadow: 0 20px 60px rgba(0,0,0,0.3);
            padding: 40px;
            max-width: 800px;
            width: 100%;
        }
        h1 {
            color: #333;
            margin-bottom: 10px;
            font-size: 2em;
            text-align: center;
        }
        .subtitle {
            color: #666;
            margin-bottom: 30px;
            font-size: 1.1em;
            text-align: center;
        }
        .info-box {
            background: #f8f9fa;
            border-left: 4px solid #667eea;
            padding: 20px;
            margin: 20px 0;
            border-radius: 5px;
        }
        .info-box h3 {
            color: #667eea;
            margin-bottom: 15px;
        }
        .step {
            margin: 15px 0;
            padding-left: 30px;
            position: relative;
        }
        .step::before {
            content: "✓";
            position: absolute;
            left: 0;
            top: 0;
            width: 24px;
            height: 24px;
            background: #667eea;
            color: white;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 14px;
        }
        .btn {
            display: inline-block;
            padding: 15px 40px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            text-decoration: none;
            border-radius: 50px;
            font-size: 1.1em;
            font-weight: 600;
            transition: transform 0.3s;
            margin: 10px;
            border: none;
            cursor: pointer;
        }
        .btn:hover {
            transform: translateY(-2px);
        }
        .btn-green {
            background: linear-gradient(135deg, #28a745 0%, #20c997 100%);
        }
        .btn-blue {
            background: linear-gradient(135deg, #0d6efd 0%, #60a5fa 100%);
        }
        .warning {
            background: #fff3cd;
            border-left: 4px solid #ffc107;
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
            color: #856404;
        }
        .success {
            background: #d4edda;
            border-left: 4px solid #28a745;
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
            color: #155724;
        }
        .error {
            background: #f8d7da;
            border-left: 4px solid #dc3545;
            padding: 15px;
            margin: 20px 0;
            border-radius: 5px;
            color: #721c24;
        }
        .toast {
            position: fixed;
            top: 20px;
            right: 20px;
            padding: 10px 20px;
            border-radius: 4px;
            color: white;
            font-weight: 500;
            box-shadow: 0 2px 10px rgba(0,0,0,0.2);
            display: none;
            z-index: 1000;
            animation: fadeInOut 3s;
        }
        .toast-success {
            background-color: #28a745;
        }
        .toast-error {
            background-color: #dc3545;
        }
        @keyframes fadeInOut {
            0% { opacity: 0; }
            15% { opacity: 1; }
            85% { opacity: 1; }
            100% { opacity: 0; }
        }
        .actions {
            text-align: center;
            margin: 30px 0;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔐 OAuth2 授权引导</h1>
        <p class="subtitle">使用OAuth2授权码模式访问消息管理系统</p>
        
        <!-- 如果有错误信息，显示错误提示 -->
        <div th:if="${errorMessage}" class="error">
            <strong>❌ 错误：</strong> <span th:text="${errorMessage}">授权过程中发生错误</span>
        </div>
        
        <!-- 成功消息 -->
        <div th:if="${successMessage}" class="success">
            <strong>✅ 成功：</strong> <span th:text="${successMessage}">操作成功</span>
        </div>
        
        <div class="info-box">
            <h3>📋 授权流程说明</h3>
            <div class="step">点击下方"开始授权"按钮</div>
            <div class="step">使用 <code>user/password</code> 或 <code>admin/password</code> 登录授权服务器</div>
            <div class="step">授权成功后自动获取访问令牌并进入消息管理系统</div>
            <div class="step">使用获取到的令牌管理消息资源（增删改查）</div>
        </div>
        
        <div class="actions">
            <!-- 检查是否已有token -->
            <div id="unauthorizedPanel">
                <a href="http://localhost:9000/oauth2/authorize?response_type=code&client_id=oauth2-client&scope=openid%20profile%20message.read%20message.write&redirect_uri=http://localhost:8080/callback&state=xyz123" 
                   class="btn">
                    🚀 开始 OAuth2 授权
                </a>
            </div>
            
            <div id="authorizedPanel" style="display: none;">
                <a href="/messages" class="btn btn-green">
                    📝 进入消息管理系统
                </a>
                <button id="logoutBtn" class="btn">退出登录</button>
            </div>
        </div>
        
        <div class="warning">
            <strong>⚠️ 注意事项：</strong>
            <ul style="margin-top: 10px; padding-left: 20px;">
                <li>确保授权服务器(9000端口)和资源服务器(8090端口)已启动</li>
                <li>测试账号: <code>user/password</code> 或 <code>admin/password</code></li>
                <li>默认令牌有效期为1小时，刷新令牌有效期为30天</li>
            </ul>
        </div>
    </div>

    <!-- 提示框 -->
    <div id="toast" class="toast"></div>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 检查本地存储中是否有令牌
            const accessToken = localStorage.getItem('oauth2_access_token');
            const refreshToken = localStorage.getItem('oauth2_refresh_token');
            
            // 检查URL参数
            const urlParams = new URLSearchParams(window.location.search);
            const authorized = urlParams.get('authorized');
            
            // 如果有授权成功标记且有token，自动跳转到消息管理页
            if (authorized === 'true' && accessToken) {
                setTimeout(() => {
                    window.location.href = '/messages';
                }, 1000); // 给一点时间让用户看到首页
            }
            
            // 如果有token，显示已授权面板
            if (accessToken) {
                document.getElementById('unauthorizedPanel').style.display = 'none';
                document.getElementById('authorizedPanel').style.display = 'block';
                showToast('已授权，可以进入消息管理系统', 'success');
            }
            
            // 绑定退出登录按钮事件
            document.getElementById('logoutBtn')?.addEventListener('click', function() {
                if (confirm('确定要退出登录吗？')) {
                    localStorage.removeItem('oauth2_access_token');
                    localStorage.removeItem('oauth2_refresh_token');
                    localStorage.removeItem('oauth2_token_type');
                    localStorage.removeItem('oauth2_scope');
                    localStorage.removeItem('oauth2_expires_in');
                    
                    showToast('已退出登录', 'success');
                    setTimeout(() => {
                        window.location.reload();
                    }, 1000);
                }
            });
            
            // 清除URL参数
            if (urlParams.has('authorized') || urlParams.has('error')) {
                const url = new URL(window.location);
                url.searchParams.delete('authorized');
                url.searchParams.delete('error');
                window.history.replaceState({}, '', url);
            }
            
            /**
             * 显示提示信息
             */
            function showToast(message, type) {
                const toast = document.getElementById('toast');
                toast.textContent = message;
                toast.className = 'toast toast-' + type;
                toast.style.display = 'block';
                
                setTimeout(() => {
                    toast.style.display = 'none';
                }, 3000);
            }
        });
    </script>
</body>
</html>